<?php

date_default_timezone_set('UTC');

require_once("../data/conf/config.php");
require_once("modules/Module.class.php");

$dsn = sprintf('mysql:host=%s;dbname=%s',$mysql_host, $mysql_db);
$dbh = new PDO($dsn,$mysql_user,$mysql_pass);

function main () {
	try {
		$module = "";
		$version = "latest";
	
		if ( isset($_GET["module"]) )	$module = filter_var($_GET["module"], FILTER_SANITIZE_STRING);
		if ( isset($_GET["version"]) )	$version = filter_var($_GET["version"], FILTER_SANITIZE_STRING);
	
	
		$module_loader = sprintf("modules/%s/%s/%s_module.php", $module, $version, $module);
		if ( file_exists($module_loader) ) {
			require_once($module_loader);
			if ( class_exists($module) ) {
				$m = new $module();
				$m->run();
			} else {
				Header($_SERVER["SERVER_PROTOCOL"]." 500 Internal Server Error");
				print "Malformed API class file";
				exit;
	
			}	
	
		} else {
			// Maybe we should implement error handler class that could take care of these
			// http://gif.phpnet.org/frederic/programs/http_status_codes/http_status_codes-php.txt
			Header($_SERVER["SERVER_PROTOCOL"]." 500 Internal Server Error");
			print "Requested API not found, did you even request one?";
		}
	} catch (Exception $e) {
		Header($_SERVER["SERVER_PROTOCOL"]." 500 Internal Server Error");
		print "Something went wrong with loading API module";
	}
}

main();
?>
